package com.wl.zlmanage.utils;/**
 * Created by Administrator on 2018/6/4.
 */

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 测试加解密
 */
public class DESPlus {

    private static String strDefaultKey = "www.si-tech.com.cn";
    private Cipher encryptCipher;
    private Cipher decryptCipher;
    private String outkey;

    public String getOutkey() {
        return outkey;
    }

    public void setOutkey(String outkey) {
        this.outkey = outkey;
    }

    public static String byteArr2HexStr(byte[] arrB) throws Exception {
        int intTmp = 0;
        int iLen = arrB.length;
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            for (intTmp = arrB[i]; intTmp < 0; intTmp += 256)
                ;
            if (intTmp < 16)
                sb.append("0");
            sb.append(Integer.toString(intTmp, 16));
        }

        return sb.toString();
    }

    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i += 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[(i / 2)] = (byte) Integer.parseInt(strTmp, 16);
        }

        return arrOut;
    }

    public DESPlus() throws Exception {
        this(strDefaultKey);
    }

    public DESPlus(String strKey) throws Exception {
        this.encryptCipher = null;
        this.decryptCipher = null;
//		Security.addProvider(new SunJCE());
        Key key = getKey(strKey.getBytes());
        this.encryptCipher = Cipher.getInstance("DES");
        this.encryptCipher.init(1, key);
        this.decryptCipher = Cipher.getInstance("DES");
        this.decryptCipher.init(2, key);
    }

    public byte[] encrypt(byte[] arrB) throws Exception {
        return this.encryptCipher.doFinal(arrB);
    }

    public String encrypt(String strIn) throws Exception {
        return byteArr2HexStr(encrypt(strIn.getBytes()));
    }

    public byte[] decrypt(byte[] arrB) throws Exception {
        return this.decryptCipher.doFinal(arrB);
    }

    public String decrypt(String strIn) throws Exception {
        return new String(decrypt(hexStr2ByteArr(strIn)));
    }

    private Key getKey(byte[] arrBTmp) throws Exception {
        byte[] arrB = new byte[8];
        for (int i = 0; (i < arrBTmp.length) && (i < arrB.length); i++) {
            arrB[i] = arrBTmp[i];
        }
        Key key = new SecretKeySpec(arrB, "DES");
        return key;
    }


//    public static void main(String[] args) throws Exception {
//
//        String srcStr= "login_no=A123456&region_id=11&group_id=23&busi_group_id=10008&op_code=8888&ip_address=10.112.6.18&send_time=20180605100621877&key=";
//        String key="chnflow";   //密钥约定为chnflow
//        DESPlus des = new DESPlus(key);
//        String encrystring= des.encrypt(srcStr);
//        String decrystring = des.decrypt(encrystring);
//
//        System.out.println("srcStr="+srcStr);
//        System.out.println("encrystring="+encrystring);
//        System.out.println("decrystring="+decrystring);
//    }
}
